package com.appcenter.application.service;


import com.appcenter.application.dto.users.UserAddrDto;
import com.appcenter.application.input.Useraddrs.*;
import com.appcenter.application.mapper.UserAddrsMapper;
import com.appcenter.application.model.UserAddrs;
import com.appcenter.application.output.useraddr.*;
import com.appcenter.jz.unit.ObjectUnit;
import com.appcenter.jz.page.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class UserAddrService {
    @Autowired
    UserAddrsMapper userAddrsMapper;

    public SearchUserAddrListOutput SearchUserAddrList(SearchUserAddrListInput input) throws Exception {
        SearchUserAddrListOutput output = new SearchUserAddrListOutput();

        PageHelper.startPage(input.getPage().getPageNo(), input.getPage().getPageSize(), "CreateTime");
        List<UserAddrs> ls = userAddrsMapper.searchUserAddrList();


        List<UserAddrDto> newls = new ArrayList<UserAddrDto>();
        for (int i = 0; i < ls.size(); i++) {
            UserAddrDto newDto = new UserAddrDto();
            BeanUtils.copyProperties(ls.get(i), newDto);
            newls.add(newDto);
        }
        PageInfo<UserAddrDto> page = new PageInfo<>(newls);
        output.setItems(new PagedResult<UserAddrDto>(page.getList(), (int) page.getTotal()));
        return output;
    }

    public LoadUserAddrListPageOutput LoadUserAddrListPage(LoadUserAddrListPageInput input) throws Exception {
        LoadUserAddrListPageOutput out = new LoadUserAddrListPageOutput();
        out.setItems(SearchUserAddrList(input).getItems());
        return out;
    }

    public LoadViewUserAddrPageOutput LoadViewUserAddrPage(LoadViewUserAddrPageInput input) throws Exception {
        LoadViewUserAddrPageOutput output = new LoadViewUserAddrPageOutput();

        UserAddrDto userAddrs = userAddrsMapper.getUserAddrById(input.getId());

        UserAddrDto item = new UserAddrDto();
        BeanUtils.copyProperties(userAddrs, item);
        output.setItem(item);
        return output;
    }

    public CreateUserAddrOutput CreateUserAddr(CreateUserAddrInput input) throws Exception {
        CreateUserAddrOutput output = new CreateUserAddrOutput();
        UserAddrs userAddrs = new UserAddrs();
        BeanUtils.copyProperties(input.getItem(), userAddrs);
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        userAddrs.setId(uuid);
        userAddrsMapper.insertSelective(userAddrs);
        UserAddrDto item = new UserAddrDto();
        BeanUtils.copyProperties(userAddrs, item);
        output.setItem(item);
        return output;
    }

    public LoadEditUserAddrPageOutput LoadEditUserAddrPage(LoadEditUserAddrPageInput input) throws Exception {
        LoadEditUserAddrPageOutput output = new LoadEditUserAddrPageOutput();
        UserAddrDto userAddrs = userAddrsMapper.getUserAddrById(input.getId());
        UserAddrDto item = new UserAddrDto();
        BeanUtils.copyProperties(userAddrs, item);
        output.setItem(item);
        return output;
    }

    public EditUserAddrOutput EditUserAddr(EditUserAddrInput input) throws Exception {
        EditUserAddrOutput output = new EditUserAddrOutput();
        //UserAddrs userAddrs=new UserAddrs();
        UserAddrDto userAddrs = userAddrsMapper.getUserAddrById(input.getItem().getId());
        if (userAddrs != null) {
            BeanUtils.copyProperties(input.getItem(), userAddrs);
            userAddrsMapper.editUserAddr(input.getItem());
            output.setItem(userAddrs);
            return output;
        }
        return output;
    }

    public DeleteUserAddrOutput DeleteUserAddr(DeleteUserAddrInput input) throws Exception {
        DeleteUserAddrOutput output = new DeleteUserAddrOutput();
        userAddrsMapper.deleteByPrimaryKey(input.getItem().getId());
        UserAddrs userAddrs = new UserAddrs();
        userAddrs.setId(input.getItem().getId());

        UserAddrDto item = new UserAddrDto();
        BeanUtils.copyProperties(userAddrs, item);
        output.setItem(item);
        return output;
    }
}
